#include 	"ipmsg.h"
#include 	"MyIpmsg.h"
/*----------------------------------------对用户的处理函数------------------------------------*/
ipmsg_user_list * adduser(struct sockaddr_in * useraddrs, struct ipmsguser * q, ipmsgdata * data)
{
	ipmsg_user_list * p = NULL;
	char 		buf[PATH_MAX * 2 + 1] = {'\0'};
	char		*t = NULL, *tt = NULL;
	
	p = (struct ipmsguser *) malloc(sizeof(struct ipmsguser));
	if (!p) {
		perror("malloc:");
		exit(1);
	}
	
	memcpy( &(p->user_addr), useraddrs, sizeof(struct sockaddr_in));
	strncpy(p->user_name, data->sendername, strlen(data->sendername) + 1);
	//p->user_name = strdup(data->sendername);
	strncpy(p->user_machine, data->senderhost, strlen(data->senderhost) + 1);
	//p->user_machine = strdup(data->senderhost);
	strncpy(buf, data->append, strlen(data->append) + 1);
	//buf = strdup(data->append);
	memset(p->user_group, 0, MAX_USER_GROUP_LENGTH);
	memset(p->user_nick, 0, MAX_USER_NAME_LENGTH);
	if (buf[0]) {
		tt = strchr(buf, '\a');
		t = strtok(buf, "\a");
		if (t) {
			strncpy(p->user_nick, t, strlen(t) + 1);
			//t = strtok(NULL, "\a");
			if (tt) {
				t = t + strlen(t) + 1;
				strncpy(p->user_group, t, strlen(t) + 1);
			}
		}
		//p->user_group = strtok(NULL, "\a");
	}
	else {
		//p->user_nick = NULL;
		//p->user_group = NULL;
		memset(p->user_nick, 0, MAX_USER_NAME_LENGTH);
		memset(p->user_group, 0, MAX_USER_GROUP_LENGTH);
	}
	
	p->pre = q;
	p->next = NULL;
	q->next = p;
	q = q->next;
	
	return q;
}

char user_exist(struct sockaddr_in * useraddr, ipmsgdata * data)	/*判断用户列表是否存在useraddr用户，存在返回0不存在返回1*/
{
	ipmsg_user_list * p = NULL;
	
	p = head->next;
	
	while (p) {
		if (!strcmp(p->user_name, data->sendername) && !strcmp(p->user_machine, data->senderhost) \
				&& !memcmp( &(p->user_addr), useraddr, sizeof(struct sockaddr_in)))
			return 0;
		p = p->next;
	}
	
	return 1;
}

void del_user(struct sockaddr_in * useraddr, ipmsgdata * data)	/*删除用户*/
{
	ipmsg_user_list * p = NULL;
	
	p = head->next;
	
	while (p) {
		if(!strcmp(p->user_name, data->sendername) && !strcmp(p->user_machine, data->senderhost) \
				&& !memcmp( &(p->user_addr), useraddr, sizeof(struct sockaddr_in))) {
			if(p->next == NULL) {
				p->pre->next = NULL;
				free(p);
				return;
			}
			p->pre->next = p->next;
			p->next->pre = p->pre;
			free(p);
			return;
		}
		p = p->next;
	}
	
	return;
}

void del_all_user()
{
	ipmsg_user_list * p = NULL, * q = NULL;
	
	p = head->next;
	head->next = NULL;
	tail = head;
	
	while (p) {
		q = p;
		p = p->next;
		free(q);
	}
}
//从在线用户列表中查询,可以按在线用户的序号、用户的IP地址或者用户名来进行查询，返回找到的节点的指针，找不到返回NULL
//若按在线用户的编号来查询，则后两个参数均赋值为NULL,若按IP地址查询,则第一和第三个参数为NULL，同理按用户名查询
//若都三个参数中有不止一个不为NULL，则按用户编号、IP地址、用户名的顺序来进行查询
ipmsg_user_list * search_user(int *usernum,struct in_addr *useraddr,char *username)
{
	ipmsg_user_list * p =head->next;
	
	if (usernum != NULL) {
		while (p) {
			if ( * usernum == p->user_num)
				return p;
			p = p->next;
		}
		
		return p;
	}
	else if (useraddr != NULL) {
		while (p) {
			if (!memcmp(useraddr, & (p->user_addr.sin_addr), sizeof(struct sockaddr_in)))
				return p;
			p = p->next;
		}
		
		return p;
	}
	else if (username != NULL) {
		while (p) {
			if (!strcmp(username, p->user_name))
				return p;
			p = p->next;
		}
		
		return p;
	}
	
	return NULL;
}
